home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Joy.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
6KB
|
283 lines
#include "stdafx.h"
char joy_str[kMaxJoys][MAX_PATH];
GUID *joy_guid[kMaxJoys];
DWORD joy_fire[kMaxJoys], joy_jetpack[kMaxJoys], joy_mine[kMaxJoys];
static BOOL CALLBACK DIEnumDevicesProc(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
// Check if string matches the stored description of the controller
// and if so store GUID
for (int i = 0; i < kMaxJoys; i++)
if (eq(lpddi->tszInstanceName, joy_str[i]))
joy_guid[i] = new GUID(lpddi->guidInstance);
return DIENUM_CONTINUE;
}
static BOOL CALLBACK DIEnumDevicesProc2(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
// Check if none is selected and an unused joystick is found
for (int i = 0; i < kMaxJoys; i++)
if (joy_guid[i] == 0)
{
// check if the joystick is not already used
for (int j = 0; j < kMaxJoys && !eq(lpddi->tszInstanceName, joy_str[j]); j++)
continue;
// assign it
if (j >= kMaxJoys)
{
strcpy(joy_str[i], lpddi->tszInstanceName);
joy_guid[i] = new GUID(lpddi->guidInstance);
joy_fire[i] = DIJOFS_BUTTON0;
joy_jetpack[i] = DIJOFS_BUTTON1;
joy_mine[i] = DIJOFS_BUTTON2;
}
}
return DIENUM_CONTINUE;
}
void init_joystick()
{
// Reset the GUIDs
for (int i = 0; i < kMaxJoys; i++)
joy_guid[i] = NULL;
// Search GUID for selected controllers
DI->EnumDevices(DIDEVTYPE_JOYSTICK, DIEnumDevicesProc, 0, DIEDFL_ATTACHEDONLY);
// Make sure something is selected
DI->EnumDevices(DIDEVTYPE_JOYSTICK, DIEnumDevicesProc2, 0, DIEDFL_ATTACHEDONLY);
}
void deinit_joystick()
{
// Delete GUIDs
for (int i = 0; i < kMaxJoys; i++)
safe_delete(&joy_guid[i]);
}
LPDIRECTINPUTDEVICE2 create_input_device_joystick(GUID *guid)
{
LPDIRECTINPUTDEVICE2 joystick;
// Create DirectInput device
joystick = create_input_device(*guid);
if (FAILED(joystick->SetCooperativeLevel(mainwindowhandle, DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
error("Unable to set cooperative level for joystick");
if (FAILED(joystick->SetDataFormat(&c_dfDIJoystick)))
error("Unable to set data format to joystick");
// Set range
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(diprg);
diprg.diph.dwHeaderSize = sizeof(diprg.diph);
diprg.diph.dwObj = 0;
diprg.diph.dwHow = DIPH_DEVICE;
diprg.lMin = JOY_MIN;
diprg.lMax = JOY_MAX;
if (FAILED(joystick->SetProperty(DIPROP_RANGE, &diprg.diph)))
error("Unable to set range for joystick");
// Set deadzone
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = JOY_DEAD;
if (FAILED(joystick->SetProperty(DIPROP_DEADZONE, &dipdw.diph)))
error("Unable to set dead zone for joystick");
return joystick;
}
cJoystick::cJoystick(int num)
{
// Create joystick device
if (joy_guid[num] == 0)
joystick = 0;
else
joystick = create_input_device_joystick(joy_guid[num]);
// Create button offsets
fire = joy_fire[num] == 0xffffffff? 0 : (BYTE *)&state + joy_fire[num];
jetpack = joy_jetpack[num] == 0xffffffff? 0 : (BYTE *)&state + joy_jetpack[num];
mine = joy_mine[num] == 0xffffffff? 0 : (BYTE *)&state + joy_mine[num];
}
cJoystick::~cJoystick()
{
if (joystick != 0)
{
joystick->Unacquire();
joystick->Release();
}
}
void cJoystick::reset()
{
}
void cJoystick::determine(cPlayer *p)
{
// Check if this joystick is present
if (joystick == 0)
return;
// Poll this joystick
if (FAILED(joystick->Poll()))
{
// Joystick input was lost, reacquire
joystick->Acquire();
if (FAILED(joystick->Poll()))
return;
}
// Get the state of the joystick
if (FAILED(joystick->GetDeviceState(sizeof(state), &state)))
{
// Joystick input was lost, reacquire
joystick->Acquire();
if (FAILED(joystick->GetDeviceState(sizeof(state), &state)))
return;
}
// Make discrete states
int left = state.lX < 0,
right = state.lX > 0,
up = state.lY < 0,
down = state.lY > 0,
f = fire == 0? FALSE : *fire & 0x80;
// Cannot do anything when not active
if (!p->is_active())
return;
// Check fire button
if (mine == 0? FALSE : *mine & 0x80)
{
p->fire_down();
}
else if (f)
{
if (up)
p->fire_up();
else if (down && (left || right))
p->fire_down();
else
p->fire();
}
// Cannot do anything more when captured
if (p->is_captured())
return;
// Jetpack
if (jetpack == 0? FALSE : *jetpack & 0x80)
p->jet_on();
else
p->jet_off();
// Do movement
if (p->is_walking())
{
if (left)
p->walk_left();
else if (right)
p->walk_right();
else
p->walk_h_halt();
if (up && !f)
p->jump();
else if (down)
p->duck();
}
else if (p->is_ducked())
{
if (!down)
p->stand();
if (left)
p->duck_left();
else if (right)
p->duck_right();
}
else if (p->is_jumping())
{
if (left)
p->jump_left();
else if (right)
p->jump_right();
else
p->jump_h_halt();
}
else if (p->is_jetting())
{
if (up)
p->jet_up();
else if (down)
p->jet_down();
else
p->jet_v_halt();
if (left)
p->jet_left();
else if (right)
p->jet_right();
else
p->jet_h_halt();
}
else if (p->is_climbing())
{
if (up)
p->climb_up();
else if (down)
p->climb_down();
else
p->climb_v_halt();
if (left)
p->climb_left();
else if (right)
p->climb_right();
else
p->climb_h_halt();
}
}